package com.audaque.springboot.foshanupload.casclientautoconfigsupport.strategy;

import com.audaque.springboot.foshanupload.core.result.R;
import com.audaque.springboot.foshanupload.core.enums.ResultCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jasig.cas.client.authentication.AuthenticationRedirectStrategy;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Data
@NoArgsConstructor
@Component
public class UnAuthenticationRedirectStrategy implements AuthenticationRedirectStrategy {
    //这里不支持依赖注入
    @Override
    public void redirect(HttpServletRequest request, HttpServletResponse response, String s) throws IOException {
        //返回json，让前端自己决定路由到哪个页面
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        PrintWriter out = response.getWriter();

        R r = new R();
        r.setCode(ResultCodeEnum.NOT_LOGIN.getCode());
        r.setSuccess(false);
        r.setMsg(ResultCodeEnum.NOT_LOGIN.getMsg());
        out.write(r.toString());
    }
}
